package com.grape.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.grape.domain.AccountSetting;
import com.grape.mapper.AccountSettingMapper;
import com.grape.service.IAccountSettingService;
import com.grape.constants.Constants;
import com.grape.utils.StringUtils;
import com.grape.utils.ThreadLocalUtil;
import com.grape.vo.BaseVo;
import com.grape.wrapper.BaseWrapper;
import lombok.AllArgsConstructor;
import org.apache.logging.log4j.util.Strings;
import org.slf4j.Logger;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 *
 * 账户配置表 服务实现类
 *
 * @author      putao
 * @email       2628279194@qq.com
 */
@Service
@AllArgsConstructor
@Transactional(readOnly = true)
public class AccountSettingServiceImpl extends ServiceImpl<AccountSettingMapper, AccountSetting> implements IAccountSettingService {

    private final AccountSettingMapper accountSettingMapper;

    private static final Logger logger = org.slf4j.LoggerFactory.getLogger(AccountSettingServiceImpl.class);

    /**
     * 分页
     * @param baseVo    统一分页对象
     * @return          账户配置表集合
     */
    @Override
    public List<AccountSetting> listData(BaseVo baseVo) {
        QueryWrapper<AccountSetting> wrapper = new BaseWrapper<AccountSetting>().init();
        wrapper.like(Strings.isNotBlank(baseVo.getSearch()), Constants.NAME, baseVo.getSearch())
                .eq(StringUtils.isNotBlank(baseVo.getColumnName()) && StringUtils.isNotBlank(baseVo.getSelect()), baseVo.getColumnName(), baseVo.getSelect());
        orderByModifyTimeDesc(wrapper);
        return this.list(wrapper);
    }

    /**
     * 自定义分页
     * @param   baseVo      统一分页对象
     */
    @Override
    public void pageListCustom(BaseVo baseVo) {
        String userId = ThreadLocalUtil.getUserId();
        QueryWrapper<AccountSetting> qw = new QueryWrapper<AccountSetting>().eq(Constants.USER_ID, userId)
                .orderByDesc(Constants.CREATE_TIME);
        this.accountSettingMapper.selectList(qw);
    }

    /**
     * 修改
     * @param accountSetting 账户配置表对象
     */
    @Override
    @Transactional(readOnly = false, rollbackFor = Exception.class)
    public void updateData(AccountSetting accountSetting) {
        this.baseUpdate(accountSetting);
    }

    /**
     * 新增
     * @param       accountSetting 账户配置表对象
     * @return      账户配置表对象
     */
    @Override
    @Transactional(readOnly = false, rollbackFor = Exception.class)
    public AccountSetting insertData(AccountSetting accountSetting) {
        return this.baseInsert(accountSetting);
    }

    /**
     * 删除
     * @param id    单据id
     * @return      删除条数
     */
    @Override
    @Transactional(readOnly = false, rollbackFor = Exception.class)
    public int deleteData(String id) {
        return this.baseDelete(id);
    }

    /**
     * 根据id查询详情
     * @param       id  单据id
     * @return      账户配置表对象
     */
    @Override
    public AccountSetting detail(String id) {
        return this.getByIdForDelFlag(id);
    }

    /**
     * 获取所有数据
     * @return  账户配置表集合
     */
    @Override
    public List<AccountSetting> listAll() {
        return accountSettingMapper.selectList(new QueryWrapper<AccountSetting>().eq(Constants.DEL_FLAG, Constants.ZERO_STR));
    }
}
